Lots of people use months to calculate the age of their young child. But, why limit yourself to months when you could tell people how old your baby is in seconds, or days, or centuries, or jubilees?
This was purely made for the joy of explaining your baby's age in a weird random value of time other than months or years (but also includes calculations for months and years). It's not super accurate, but it's a pretty close approximation based on a birth day and time.
Approximate Age |
---|
Here's all the JavaScript code. Even the sketchy way the click to copy works by creating a text element, assigning it a value, copying that value, then deleting the element, which is honestly more cool than the actual age calculations.
// submit all fields and start the process of calculating age
function submitFields(){
resetPage();
let date = document.getElementById('date').value.split("-"),
time = document.getElementById('time').value.split(":"),
measure = document.getElementById('measure').value,
currentDateTime = Date.now();
if ((date[0] === '' || date === 'undefined') || (time[0] === '' || time == 'undefined')) {
alert('Invalid date or time.');
return;
}
let selectedDateTime = new Date(date[0],date[1]-1,date[2],time[0],time[1],time[2]);
if (currentDateTime < selectedDateTime) {
alert('Date/Time cannot be greater than the current date and time.');
return;
}
let diff = Math.abs(currentDateTime - selectedDateTime) * 0.001; //diff in seconds
switch (measure) {
case 'seconds':
ageCell({seconds: diff});
break;
case 'minutes':
ageCell({minutes: minutes(diff)});
break;
case 'moments':
ageCell({moments: moments(diff)});
break;
case 'hours':
ageCell({hours: hours(diff)});
break;
case 'days':
ageCell({days: days(diff)});
break;
case 'weeks':
ageCell({weeks: weeks(diff)});
break;
case 'fortnights':
ageCell({fortnights: fortnights(diff)});
break;
case 'months':
ageCell({months: months(diff)});
break;
case 'protege':
ageCell({protege: protege(diff)});
break;
case 'years':
ageCell({years: years(diff)});
break;
case 'decades':
ageCell({decades: decades(diff)});
break;
case 'jubilees':
ageCell({jubilees: jubilees(diff)});
break;
case 'centuries':
ageCell({centuries: centuries(diff)});
break;
case 'millennia':
ageCell({millennia: millennia(diff)});
break;
case 'all':
ageCell(all(diff));
break;
default:
ageCell(all(diff));
break;
}
}
/*
* block of functions that determine the calculations of each age.
* in many cased functons call another function to get a previously determined
* value to be used for age calculations
*/
function minutes(diff){
return diff / 60;
}
function moments(diff){
return diff / 90;
}
function hours(diff){
return minutes(diff) / 60;
}
function days(diff){
return hours(diff) / 24;
}
function weeks(diff){
return days(diff) / 7;
}
function fortnights(diff){
return days(diff) / 14;
}
function months(diff){
return days(diff) / 30.42;
}
function years(diff){
return days(diff) / 365;
}
function protege(diff){
return days(diff) / 323;
}
function decades(diff){
return years(diff) / 10;
}
function jubilees(diff){
return years(diff) / 25;
}
function centuries(diff){
return years(diff) / 100;
}
function millennia(diff){
return years(diff) / 1000;
}
function all(diff){
return {
seconds: diff,
minutes: minutes(diff),
days: days(diff),
weeks: weeks(diff),
fortnights: fortnights(diff),
months: months(diff),
years: years(diff),
decades: decades(diff),
protege: protege(diff),
jubilees: jubilees(diff),
centuries: centuries(diff),
millennia: millennia(diff)
};
}
// create the cell for each age
function ageCell(obj){
let i = 0,
cell = {
"age": "",
};
for(o in obj){
cell.id = i;
cell.age = `${obj[o]} ${o}`;
cell.units = o
drawAgeTable(cell);
i++;
}
// initializes tooltip? needed to be called to get the tool tip css to trigger
$('[data-toggle="tooltip"]').tooltip();
}
// create the table row and populate it with a cell
function drawAgeTable(cell){
if (cell === null) return;
const tabBody=document.getElementById("tabBody");
let tableRow = tabBody.insertRow();
tableRow.id = cell.id;
let cell0 = tableRow.insertCell();
cell0.setAttribute('data-toggle', 'tooltip');
cell0.setAttribute('data-placement', 'top');
cell0.setAttribute('title', `Click the cell to copy the age in ${o}.`);
cell0.setAttribute('onclick', `copyTextArea('${cell.age}');`);
let text0 = document.createTextNode(cell.age);
cell0.appendChild(text0);
}
// reset the table when the submit button is pressed and clear all elements
// the hard coded loop was for simlification in this case, but isn't a great idea
function resetPage(){
for(let i = 0; i < 12; i++){
let row = document.getElementById(i);
if (row)
row.parentNode.removeChild(row);
}
}
// create a text element, copy text, then remove the element in order to copy from a
function copyTextArea(age){
let text = document.createElement('input');
text.setAttribute('type', 'text');
text.value = age;
document.body.appendChild(text);
text.select();
document.execCommand('copy');
document.body.removeChild(text);
}